<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Models\Banner;
use App\Models\Noticle;
use App\Models\CouponType;
use App\Models\Goods;

class IndexController extends Controller
{
    public function banner_list(Banner $banner){
        $Banner = $banner->orderBy('sort','desc')->take(3)->get();
        return $this->success($Banner);
    }

    public function noticle_list(Noticle $noticle){
        $data = $noticle
            ->select(['id','notice_title'])
            ->orderBy('created_at','desc')
            ->simplePaginate(20);
        return $this->success($data);
    }

    public function coupon_type_list(Request $request,CouponType $couponType){
        $row = !empty($request->input('row'))?$request->input('row'):8;
        $data = $couponType
            ->orderBy('sort','desc')
            ->simplePaginate($row);
        return $this->success($data);
    }

    public function goods_list(Request $request,Goods $goods){
        $row = !empty($request->input('row'))?$request->input('row'):8;

        $query = $goods->where('is_opend',1);
        if($request->input('type_id')){
            $query->where('type_id',$request->input('type_id'));
        }
        $data = $query
            ->orderBy('sort','desc')
            ->simplePaginate($row);
        return $this->success($data);
    }
}
